<?php
namespace App\Services;

use App\Models\Ad;
use App\Models\AdPosition;
use Illuminate\Support\Facades\DB;

/**
 *
 * 广告服务层
 *
 * @author manong
 *
 */
class AdService extends BaseService {

    /**
     *  获取指定广告位的轮播图片
     *
     * @param $ad_position 广告位
     * @param int $firstrow
     * @param int $pagesize
     * @return mixed
     */
    public function getCarousels($ad_position,$firstrow=0,$pagesize=5){

        //本此系统暂无ad_positon等表，固值定死
        $advertise_info = Ad::where('position_id',$ad_position)
            ->where('enabled',1)
            ->orderBy('sort_order','asc')->skip($firstrow)->take($pagesize)
            ->get();

        if($advertise_info){
            foreach($advertise_info as $item){
                $item->pic_url = complete_url($item->pic_url);
            }

            return $advertise_info->toArray();
        } else {
            return [];
        }

    }

    /**
     *  获取指定广告位的轮播图片
     *
     * @param $position_code 广告位code
     * @param int $firstrow
     * @param int $pagesize
     * @return mixed
     */
    public function getCarouselsByCode($position_code,$firstrow=0,$pagesize=5){

        $position = AdPosition::where('position_code',$position_code)
            ->first();

        if(!$position){
            return $this->failure('广告位不存在');
        }

        return $this->getCarousels($position->position_id,$firstrow,$pagesize);

    }

    public function getCityAdByCode($province,$city,$country,$position_code,$firstrow=0,$pagesize=8){

        $position = AdPosition::where('position_code',$position_code)
            ->first();

        if(!$position){
            return $this->failure('广告位不存在');
        }


        //本此系统暂无ad_positon等表，固值定死
        $query = Ad::where('position_id',$position->position_id)
                ->where('enabled',1);
        if($province && $city && empty($country)){
            $query->where('region_name',$province.$city);
        }elseif($province && $city && $country){
            $query->where('region_name',$province.$city.$country);
        }else{
            $query->where('region_name','like',"%杭州市") ;
        }
//
        $advertise_info = $query ->orderBy('sort_order','asc')
                    ->skip($firstrow)->take($pagesize)
                    ->get();

//        if(!$advertise_info){
//            //若起不到则默认获取
//            $advertise_info = Ad::where('position_id',$position->position_id)
//                ->where('region_name','like',"%杭州市")
//                ->where('enabled',1)
//                ->orderBy('sort_order','asc')->skip($firstrow)->take($pagesize)
//                ->get();
//        }

        if($advertise_info){
            foreach($advertise_info as $item){
                $item->img_url = complete_url($item->img_url);
            }

            return $advertise_info->toArray();
        } else {
            return [];
        }

    }

//    public function getCountryAdByCode($country, $position_code,$firstrow=0,$pagesize=5){
//
//        $position = AdPosition::where('position_code',$position_code)
//            ->first();
//
//        if(!$position){
//            return $this->failure('广告位不存在');
//        }
//
//        //本此系统暂无ad_positon等表，固值定死
//        $advertise_info = Ad::where('position_id',$position->position_id)
//            //TODO 按地区显示不同内容
//            //->where('region_name','like',"%$country")
//            ->where('enabled',1)
//            ->orderBy('sort_order','asc')->skip($firstrow)->take($pagesize)
//            ->get();
//
//        if($advertise_info){
//            foreach($advertise_info as $item){
//                $item->img_url = complete_url($item->img_url);
//            }
//
//            return $advertise_info->toArray();
//        } else {
//            return [];
//        }
//
//    }

    public function getStoreAdByStoreId($store_id,$position_code,$firstrow,$pagesize){

        //广告位置还是系统统一定
        $position =DB::table('ad_position')
            ->where('store_id',0)
            ->where('position_code',$position_code)
            ->first();

        if(!$position){
            return $this->failure('广告位不存在');
        }

        $datas = DB::table('store_ad')
            ->where('position_id',$position->position_id)
            ->where('store_id',$store_id)
            ->where('enabled',1)
            ->orderBy('sort_order','asc')
            ->skip($firstrow)->take($pagesize)
            ->get();

        foreach($datas as $item){
            $ad_link = substr(trim($item->ad_link),0,4);
            if($ad_link !='http'){
                $item->ad_link = config('maqu.WAP_URL').'/'.$item->ad_link;
            }
            $item->img_url = complete_url($item->img_url);
            $item->start_time = date('y'.'年'.'m'.'月'.'d'.'日',$item->start_time);
            $item->end_time = date('y'.'年'.'m'.'月'.'d'.'日',$item->end_time);;
        }

        return $this->success($datas);

    }
}